#include "PlatoonScoutState.h"

PlatoonScoutState::PlatoonScoutState(Context* context) : State(context), mState(PlatoonScoutState::AlignAgain)
{
	BWAPI::TilePosition initPos = BWAPI::Broodwar->self()->getStartLocation();
	
	mFacingAngle = (initPos.x() < 30)? 1 : -1;
	mStep = SCOUT_STEP;
}

PlatoonScoutState::~PlatoonScoutState(void)
{
}

void PlatoonScoutState::handle()
{
	//BWAPI::Broodwar->printf("PlatoonScoutState");

	// If enemy has been seen
	if (EnemyManager::self() && EnemyManager::self()->hasVisibleUnits())
	{
		mContext->changeState(new PlatoonAttackState(mContext));
	}

	else
	{
		switch(mState)
		{
		case AlignAgain:
			alignAgain();
			break;

		case Scout:
			scout();
			break;

		default:
			break;
		}
	}
}

void PlatoonScoutState::alignAgain()
{
	Platoon* platoon = static_cast<Platoon*>(mContext);

	if(!platoon->isUnitMoving())
	{
		platoon->goLineFormation(0);
		mState = Scout;
	}
}

void PlatoonScoutState::scout()
{
	Platoon* platoon = static_cast<Platoon*>(mContext);

	if(!platoon->isUnitMoving())
	{
		BWAPI::Position newPosition(mFacingAngle*mStep, 0); //FIXME hard-coded position
		platoon->move(newPosition, true);
	}
}

